<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;;

use GuzzleHttp\Promise;

$GLOBALS['appID'] = "219AAA33E4D569E637426A7115CC8DAA";
$GLOBALS['appKey'] = '45661418513867396';
$GLOBALS['url'] = 'https://area78-win.pospal.cn:443/';

//遍历
function get_post($url, $data, $headers)
{
    $client = new Client();
    try {
        $response = $client->post($GLOBALS['url'] . $url, [
            'headers' => $headers,
            'json' => $data
        ]);
        // 输出请求结果
        return $response->getBody();
    } catch (RequestException $e) {
        // 输出请求异常
        return $e->getMessage();
    }
}

$phone = $_POST['number'];
// $store = $_POST['store'];
$startTime = $_POST['startTime'];
$endTime = $_POST['endTime'];

$variable = [
    'Atelier001' => [
        'store' => 'Atelier001',
        'url' => 'pospal-api2/openapi/v1/customerOpenApi/queryByNumber',
        'appkey' => '45661418513867396',
        'appid' => '219AAA33E4D569E637426A7115CC8DAA',
        'body' => [
            "appId" => '219AAA33E4D569E637426A7115CC8DAA',
            "customerNum" => $phone,
        ],
        'headers' => [
            'User-Agent' => 'openApi',
            'Content-Type' => 'application/json',
            'data-signature' => ''
        ]
    ],
    'AtelierJFB' => [
        'store' => 'AtelierJFB',
        'url' => 'pospal-api2/openapi/v1/customerOpenApi/queryByNumber',
        'appkey' => '860865420322249048',
        'appid' => 'D03D1586EE6DF9540A1B7B86D8C229FA',
        'body' => [
            "appId" => 'D03D1586EE6DF9540A1B7B86D8C229FA',
            "customerNum" => $phone,
        ],
        'headers' => [
            'User-Agent' => 'openApi',
            'Content-Type' => 'application/json',
            'data-signature' => ''
        ]
    ],
    'JYJFB888888' =>  [
        'store' => 'JYJFB888888',
        'url' => 'pospal-api2/openapi/v1/customerOpenApi/queryByNumber',
        'appkey' => '57896692810169407',
        'appid' => 'E551304FF510CDD9B5173B9AB0917BEE',
        'body' => [
            "appId" => 'E551304FF510CDD9B5173B9AB0917BEE',
            "customerNum" => $phone,
        ],
        'headers' => [
            'User-Agent' => 'openApi',
            'Content-Type' => 'application/json',
            'data-signature' => ''
        ]
    ]
];
foreach ($variable as $key => $value) {
    $value['headers']['data-signature'] = strtoupper(md5($value['appkey'] . json_encode($value['body'])));
    $res = json_decode(get_post($value['url'], $value['body'], $value['headers']));
    die(var_dump($res));
    if ($res->status == 'success' && !empty($res->data)) {
        // $val = $res->data;
        $val['categoryName'] = $res->data[0]->categoryName;
        $val['number'] = $res->data[0]->number;
        $val['name'] = $res->data[0]->name;
        $val['balance'] = $res->data[0]->balance;
        $val['phone'] = $res->data[0]->phone;
        $val['point'] = $res->data[0]->point;
        $val['discount'] = $res->data[0]->discount;
        $val['createdDate'] = $res->data[0]->createdDate;
        $val['extInfo'] = $res->data[0]->extInfo;
        // $val[$value['store']] = $res->data;
        //订单
        $body = [
            "appId" => $value['body']['appId'],
            "customerNum" => $res->data[0]->number
        ];
        $value['headers']['data-signature'] = strtoupper(md5($value['appkey'] . json_encode($body)));
        $order = json_decode(get_post('pospal-api2/openapi/v1/customerOpenApi/generatePayToken', $body, $value['headers']));
        die(var_dump($order));
        // $res->data[0]->order = json_decode(get_post('pospal-api2/openapi/v1/ticketOpenApi/queryCustomerHistoryTicketsByUid', $body, $value['headers']));
        if ($order->status == 'success' && !empty($order->data->result)) {
            $val['order'] = $order->data->result;
        }
        $resdata = $val;
        break;
    } else {
        $resdata = [];
    }
}
echo json_encode([
    'code' => 1,
    'data' => $resdata
]);
